import getLastEvent from '../utils/getLastEvent'
import getSelector from '../utils/getSelector'
import getStack from '../utils/getStack'
import request from '../utils/request'
export function injectJsError(){
    window.addEventListener('error',function (event) {
        let lastEvent = getLastEvent();
        console.log('in error listener');
        console.log(event);
        if(event.target?.src || event.target?.src == ""){
            request.send({
                kind: 'stability',//大分类
                type: 'error',//小类型
                errorType: 'resourceError',//小小类型
                message: 'Resources load error',//报错信息
                filename: event.target.src,
                position: event.target.tagName,
                stack: `at ${event.target.outerHTML}`,
                selector: getSelector(event)
            }).then(res => {
                console.log(res);
            })
        }else {
            request.send({
                kind: 'stability',//大分类
                type: 'error',//小类型
                errorType: 'jsError',//小小类型
                message: event.message,//报错信息
                filename: event.filename,
                position: `${event.lineno}:${event.colno}`,
                stack: event.error.stack,
                selector: getSelector(lastEvent)
            }).then(res => {
                console.log(res);
            })
        } 
    },true);
}